package com.sgb.kjwl.view.widget.transport;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AppQuotedDetailPopLayoutBinding;
import com.sgb.kjwl.model.entity.self.OrderQuoteEntity;
import com.sgb.kjwl.utils.LonLatUtils;
import com.sgb.kjwl.utils.NumberUtil;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.ui.activity.transport.BiddingOrderDetailsActivity;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.util.StringUtil;

/**
 * Describe:    报价单位-地图页面-报价详情弹窗(竞价货源)
 * Author:  yangLei
 * Date:    2021/8/27
 */
public class QuotedDetailPopWindow extends BasePopWindow implements View.OnClickListener {

    private OrderQuoteEntity offerEntity;
    private AppQuotedDetailPopLayoutBinding mBinding;

    public OrderQuoteEntity getOfferEntity() {
        return offerEntity;
    }

    public void setOfferEntity(OrderQuoteEntity offerEntity) {
        this.offerEntity = offerEntity;
        loadingOfferInfo(offerEntity);
    }

    public QuotedDetailPopWindow(Context context) {
        super(context, DIR_DOWN_UP);

    }

    @Override
    protected int popLayout() {
        return R.layout.app_quoted_detail_pop_layout;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mBinding = (AppQuotedDetailPopLayoutBinding) holder.getmBinding();
        mBinding.setOnClick(this::onClick);
    }

    /**
     * 加载报价详情
     *
     * @param entity
     */
    private void loadingOfferInfo(OrderQuoteEntity entity) {
        if (!TextUtils.isEmpty(entity.getOfferType())) {
            //头像
            if (!TextUtils.isEmpty(entity.getLogo())) {
                Glide.with(mContext).load(entity.getLogo())
                        .placeholder(R.drawable.icon_default_profile_photo_round)
                        .into(mBinding.offerUserIcon);
            }

            //运费报价
            if (!TextUtils.isEmpty(entity.getFreight())) {
                mBinding.offerPrice.setText(NumberUtil.trim0(entity.getFreight()) + "元/吨");
            }
            //距离
            double distance = 0.0d;
            if (!TextUtils.isEmpty(entity.getDistance())) {
                distance = Double.parseDouble(entity.getDistance());
            } else if (entity.getLongitude() != null && entity.getLongitude() > 0
                    && entity.getLatitude() != null && entity.getLatitude() > 0) {
                distance = LonLatUtils.getDistance(entity.getLongitude(), entity.getLatitude(),
                        NumberUtil.convertToDouble(SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "")),
                        NumberUtil.convertToDouble(SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, ""))
                );
            }
            if (distance <= 0) {
                mBinding.offerDistance.setText("/");
            } else {
                mBinding.offerDistance.setText(distance < 1 ? "1km以内" : StringUtil.formatUpDistance(String.valueOf(distance)) + "km");
            }

            //地址
            String address = "";
            //名字
            String title = "";
            //1 企业 2 团队 3 个人
            switch (entity.getOfferType()) {
                case "1":
                    if (!TextUtils.isEmpty(entity.getCompanyNo())
                            && !TextUtils.isEmpty(UserOperating.getInstance().getTeamOrCompanyNameNo())
                            && entity.getCompanyNo().equals(UserOperating.getInstance().getTeamOrCompanyNameNo())) {
                        title = !TextUtils.isEmpty(entity.getCompanyName()) ? entity.getCompanyName() : "/";
                    } else {
                        title = !TextUtils.isEmpty(entity.getCompanyName()) ? replaceString2Star(entity.getCompanyName(), 2, 0) : "/";
                    }
                    mBinding.offerAddressHint.setText("地址");
                    if (!TextUtils.isEmpty(entity.getCompanyTeamAddress())) {
                        address = entity.getCompanyTeamAddress();
                    }
                    mBinding.offerCarInfoLayout.setVisibility(View.GONE);
                    break;

                case "2":
                    if (!TextUtils.isEmpty(entity.getTeamNo())
                            && !TextUtils.isEmpty(UserOperating.getInstance().getTeamOrCompanyNameNo())
                            && entity.getTeamNo().equals(UserOperating.getInstance().getTeamOrCompanyNameNo())) {
                        title = !TextUtils.isEmpty(entity.getTeamName()) ? entity.getTeamName() : "/";
                    } else {
                        title = !TextUtils.isEmpty(entity.getTeamName()) ? replaceString2Star(entity.getTeamName(), 2, 0) : "/";
                    }
                    mBinding.offerAddressHint.setText("地址");
                    if (!TextUtils.isEmpty(entity.getCompanyTeamAddress())) {
                        address = entity.getCompanyTeamAddress();
                    }
                    mBinding.offerCarInfoLayout.setVisibility(View.GONE);
                    break;

                case "3":
                    mBinding.offerAddressHint.setText("车辆位置");
                    if (!TextUtils.isEmpty(entity.getDetailAddress())) {
                        address = entity.getDetailAddress();
                    }

                    StringBuilder carInfo = new StringBuilder();
                    String carNum = "";
                    if (!TextUtils.isEmpty(UserOperating.getInstance().getUserNo())
                            && !TextUtils.isEmpty(entity.getUserNo())
                            && UserOperating.getInstance().getUserNo().equals(entity.getUserNo())) {
                        title = !TextUtils.isEmpty(entity.getName()) ? entity.getName() : "/";
                        carNum = !TextUtils.isEmpty(entity.getCarNumber()) ? entity.getCarNumber() : "";
                    } else {
                        title = !TextUtils.isEmpty(entity.getName()) ? entity.getName().substring(0, 1) + "师傅" : "/";
                        carNum = !TextUtils.isEmpty(entity.getCarNumber()) ? replaceString2Star(entity.getCarNumber(), 2, 0) : "";
                    }

                    carInfo.append(carNum + "(");
                    //车辆信息
                    if (!TextUtils.isEmpty(entity.getCarTypeName())) {
                        carInfo.append(entity.getCarTypeName() + "/");
                    }
                    if (!TextUtils.isEmpty(entity.getCarLenName())) {
                        carInfo.append("车长" + entity.getCarLenName() + "/");
                    }
                    if (!TextUtils.isEmpty(entity.getHeightHurdle())) {
                        carInfo.append("栏高" + NumberUtil.trim0(entity.getHeightHurdle()) + "米");
                    }
                    carInfo.append(")");
                    mBinding.offerCarInfo.setText(carInfo.toString());
                    mBinding.offerCarInfoLayout.setVisibility(View.VISIBLE);
                    break;
            }

            mBinding.offerUserName.setText(title);
            mBinding.offerAddress.setText(!TextUtils.isEmpty(address) ? address : "/");

            //判断是否已生成订单
            if (!TextUtils.isEmpty(entity.getOfferStatus()) && entity.getOfferStatus().equals("2")) {
                //报价已被同意
                mBinding.offerInfoBottomBar.setVisibility(View.VISIBLE);
                mBinding.bottomPadding.setVisibility(View.GONE);
            } else {
                mBinding.offerInfoBottomBar.setVisibility(View.GONE);
                mBinding.bottomPadding.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_btn:
                dissMiss();
                break;

            case R.id.order_details:
                if (offerEntity != null && !TextUtils.isEmpty(offerEntity.getOrderNo())) {
                    //跳转竞价订单详情
                    BiddingOrderDetailsActivity.start(mContext, offerEntity.getOrderNo(), 1);
                }
                break;
        }
    }

    /**
     * 对字符加星号处理：除前面几位和后面几位外，其他的字符以星号代替
     *
     * @param content  传入的字符串
     * @param frontNum 保留前面字符的位数
     * @param endNum   保留后面字符的位数
     * @return 带星号的字符串
     */
    public static String replaceString2Star(String content, int frontNum, int endNum) {
        if (content == null || content.trim().isEmpty())
            return "";
        int len = content.length();

        if (frontNum >= len || frontNum < 0 || endNum >= len || endNum < 0)
            return content;

        if (frontNum + endNum >= len)
            return content;

        int beginIndex = frontNum;
        int endIndex = len - endNum;
        char[] cardChar = content.toCharArray();

        for (int j = beginIndex; j < endIndex; j++) {
            cardChar[j] = '*';
        }

        return new String(cardChar);
    }
}
